﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using S7.Net;

namespace _8轴运动控制
{
    public class SiemensS7
    {
        // 0 表示导轨号，1表示机架号
        Plc myPlc = null;
        public bool Conncet(string IPAdress)
        {
            if (myPlc==null)
            {
                myPlc = new Plc(CpuType.S71200, IPAdress, 0, 1);
            }
            Disconncet();
            try
            {
                myPlc.Open();
                if (myPlc.IsConnected==true)
                {
                    return true;
                }
                else
                {
                    return false;
                }
                
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool Disconncet()
        {
            try
            {
                myPlc.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        //写M1线圈
        public bool WriteSingle(string str, bool value)
        {
            if (myPlc.IsConnected == false)
            {
                return false;
            }
            try
            {
                int num = 0;
                int address = 0;
                int offset = 0;
                StrSub(str,out num,out address,out offset);
                myPlc.Write(DataType.DataBlock, num, address, value, offset);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        //读单个M1线圈
        public bool ReadSingle(string str)
        {
            if (myPlc.IsConnected == false)
            {
                return false;
            }
            try
            {
                int num = 0;
                int address = 0;
                int offset = 0;
                StrSub(str, out num, out address, out offset);
                bool value = (bool)myPlc.Read(DataType.DataBlock, num, address, VarType.Bit, 1, (byte)offset);
                return value;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public void StrSub(string str, out int num, out int address, out int offset)
        {
            string[] strArray = str.Split(new string[] { "DB", "DB" }, StringSplitOptions.RemoveEmptyEntries);
            string str1 = strArray[0].Replace(".", "");
            string str2 = strArray[1].Substring(1, strArray[1].Length - 1);
            string[] strArray1 = str2.Split('.');
            num = Convert.ToInt32(str1);
            address = Convert.ToInt32(strArray1[0]);
            offset = Convert.ToInt32(strArray1[1]);
        }

        //读D0寄存器
        public string ReadRegister(string str)
        {
            if (myPlc.IsConnected == false)
            {
                return null;
            }
            try
            {
                int num = 0;
                int address = 0;
                int offset = 0;
                StrSub(str, out num, out address, out offset);
                var count = (byte)myPlc.Read(DataType.DataBlock, num, address, VarType.Byte, 1);
                byte[] data = myPlc.ReadBytes(DataType.DataBlock, num, address + 2, 254);
                string result11 = Encoding.Default.GetString(data);

                string a = result11.Replace("\0", "");
                return a;
            }
            catch (Exception)
            {
                return null;
            }

        }

        public bool WriteRegister(string str, int value)
        {
            if (myPlc.IsConnected == false)
            {
                return false;
            }
            try
            {
                int num = 0;
                int address = 0;
                int offset = 0;
                StrSub(str, out num, out address, out offset);
                myPlc.Write(DataType.DataBlock, num, address, value);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }



    }
}
